package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api("C端-地址簿相关接口")
@Slf4j
public class addressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping()
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBook addressBook){
        log.info("新增收货地址");

        addressBookService.add(addressBook);

        return Result.success();
    }


    /**
     * 查询登录用户所有地址
     * @return
     */
    @ApiOperation("查询登录用户所有地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddressByUserId(){
        log.info("查询登录用户所有地址");

        List<AddressBook> addressBookList = addressBookService.getAddressByUserId();

        return Result.success(addressBookList);
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("default")
    public Result<AddressBook> getDefaultAddress(){
        log.info("查询默认地址");

        AddressBook addressBook = addressBookService.getDefaultAddress();
        if(addressBook!=null)
            return Result.success(addressBook);
        return Result.error("没有查到到默认地址");
    }


    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址");

        addressBookService.updateAddress(addressBook);

        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddressById(@RequestParam Long id){
        log.info("根据id删除地址");

        addressBookService.deleteAddressById(id);

        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        log.info("根据id查询地址");

        AddressBook addressBook = addressBookService.getAddressById(id);

        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认地址");

        addressBookService.setDefaultAddress(addressBook);

        return Result.success();
    }






}
